﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using ATM.Entities;

namespace ATM.DataAccess
{
    public class SqlHelper
    {
        //Chuoi ket noi du lieu
        public static OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory.ToString() + "ATM.accdb");


        public static DataTable ExcuteQuery(String sqlCommand)
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                OleDbDataAdapter adap = new OleDbDataAdapter();
                adap.SelectCommand = new OleDbCommand(sqlCommand, connection);
                adap.Fill(ds);
            }
            catch(SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return ds.Tables[0];
        }

        public static int ExcuteNonQuery(String sqlCommand)
        {
            int value;
            try
            {
                connection.Open();
                OleDbCommand cmd = connection.CreateCommand();
                cmd.CommandText = sqlCommand;
                cmd.CommandType = CommandType.Text;
                value = cmd.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return value;
        }
    }
}
